import secrets

from flask import Flask
from models import db
# from routes import bp as main_bp
from routes.index import bp as index_bp
from routes.login import bp as login_bp
from routes.register import bp as register_bp
from routes.logout import bp as logout_bp
from routes.bottle import bp as bottle_bp
from routes.message import bp as message_bp
from config import HOSTNAME, PORT, USERNAME, PASSWORD, DATABASE

def create_app():
    app = Flask(__name__)
    app.secret_key = secrets.token_hex(16)

    app.config['SESSION_TYPE'] = 'filesystem'
    app.config['SQLALCHEMY_DATABASE_URI'] = f"mysql+pymysql://{USERNAME}:{PASSWORD}@{HOSTNAME}:{PORT}/{DATABASE}?charset=utf8"

    db.init_app(app)
    with app.app_context():
        db.create_all()

    # app.register_blueprint(main_bp)
    app.register_blueprint(index_bp)
    app.register_blueprint(login_bp)
    app.register_blueprint(register_bp)
    app.register_blueprint(logout_bp)
    app.register_blueprint(bottle_bp)
    app.register_blueprint(message_bp)

    return app

if __name__ == '__main__':
    app = create_app()
    app.run()